package cqrtplm.service.impl;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.hustcad.plm.pdm.workflow.model.entity.FlowTaskInfo;
import com.hustcad.plm.pdm.workflow.service.impl.TyplmFlowableTaskServiceImpl;
import com.ty.basic.utils.controller.OrderInfo;
import cqrtplm.entity.FlowTaskClaimDO;
import cqrtplm.mapper.FlowTaskClaimMapper;
import cqrtplm.mapper.RTTyplmFlowableTaskBugFixMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Primary
@Service
public class RTFlowableTaskServiceImpl extends TyplmFlowableTaskServiceImpl {

    @Resource
    private RTTyplmFlowableTaskBugFixMapper rtTyplmFlowableTaskBugFixMapper;

    @Resource
    private FlowTaskClaimMapper flowTaskClaimMapper;

    @Value("${filter.flowable.template.names:状态需求表审批流程}")
    private String templateName;

    /**
     * 2025/09/18 szh
     * 修复hf10 oracle中sql bug 若后续升级补丁版本 评估是否删除此方法
     *
     * @param params
     * @param orderInfo
     * @param pageNum
     * @param pageSize
     * @return
     */
//    @Override
//    public PageInfo<FlowTaskInfo> queryAllTaskByParams(Map<String, Object> params, OrderInfo orderInfo, int pageNum,
//                                                       int pageSize) {
//        //        PageMethod.startPage(pageNum, pageSize);
//        String order = "createstamp desc";
//        String queryInterAct;
//        if (Objects.nonNull(orderInfo)) {
//            queryInterAct = StringUtils.isBlank(orderInfo.getOrderBy()) ? "createstamp" : orderInfo.getOrderBy();
//            String sort = StringUtils.isBlank(orderInfo.getSort()) ? "desc" : orderInfo.getSort();
//            order = queryInterAct + " " + sort;
//        }
//
//        PageMethod.orderBy(order);
//        queryInterAct = "isQueryInterAct";
//        if (!params.containsKey(queryInterAct) || Objects.isNull(params.get(queryInterAct)) || StringUtils.isBlank(
//                params.get(queryInterAct).toString())) {
//            params.put("isQueryInterAct", true);
//        }
//
//        List<FlowTaskInfo> list = rtTyplmFlowableTaskBugFixMapper.queryAllTaskByParams(params);
//        String[] templateNames = templateName.split(";");
//        List<String> excludeWorkFlowNames = new ArrayList<>(Arrays.asList(templateNames));
//
//        List<FlowTaskClaimDO> flowTaskClaimDOList = flowTaskClaimMapper.selectAll();
//
//        // 使用字符串作为Key的Map
//        Map<String, FlowTaskClaimDO> claimMap = new HashMap<>();
//        Map<String, FlowTaskClaimDO> containerOidMap = new HashMap<>();
//        for (FlowTaskClaimDO flowTaskClaimDO : flowTaskClaimDOList) {
//            String key = flowTaskClaimDO.getKey();
//            claimMap.put(key, flowTaskClaimDO);
//            containerOidMap.put(key.split(":")[1], flowTaskClaimDO);
//        }
//        List<FlowTaskInfo> matchedList = new ArrayList<>();
//        int i = 0;
//        for (FlowTaskInfo task : list) {
//            if(excludeWorkFlowNames.contains(task.getWorkFlowName())){
//                matchedList.add(task);
//                continue;
//            }
//            String taskKey = task.getTaskDefKey() + ":" + task.getWorkFlowContainerOid();
//            if (claimMap.containsKey(taskKey)) {
//                FlowTaskClaimDO flowTaskClaimDO = claimMap.get(taskKey);
//                String userName = flowTaskClaimDO.getUserName();
//                String currentAccount = task.getCurrentAccount();
//                if(StringUtils.equals(userName,currentAccount)){
//                    matchedList.add(task);
//                    i++;
//                }
//            }else {
//                matchedList.add(task);
//                i++;
//            }
//            if(pageSize == i){
//                break;
//            }
//        }
//        return new PageInfo(matchedList);
//    }
}
